<!--
 * createor: zc
 * email: zc8023zs@163.com
 * create_time: 2020/3/14 13:46:24
 *-->
<template>
<el-container>
	<el-main>
		<el-form ref="dataForm" :model="formData" label-width="200px" style="padding:5% 5% 0 5%">
          <el-form-item>
            <el-button type="primary" icon="el-icon-edit" @click="onSave" :loading="saveLoading">{{$t('table.save')}}</el-button>
          </el-form-item> 
          <input type="hidden" v-model="formData.article_id"/>
        <el-form-item label="发布平台：">
            <el-select v-model="formData.platform" placeholder="请选择">
                <el-option
                  v-for="item in platformSelectData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="浏览设置：">
            <el-select v-model="formData.view_control" placeholder="请选择">
                <el-option
                  v-for="item in view_controlSelectData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="评论设置：">
            <el-select v-model="formData.comment_control" placeholder="请选择">
                <el-option
                  v-for="item in comment_controlSelectData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
          <input type="hidden" v-model="formData.version_id"/>
		</el-form>
	</el-main>
</el-container>
</template>
<style scoped>
body .app-container .el-container,
body .app-container .el-header,
body .app-container .el-main,
body .el-container,
body .el-header,
body .el-main {
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
</style>
<script>
import {
  article_exSelecttree,
  article_exSelectpage,
  article_exSelect,
  article_exAdd,
  article_exUpdate,
  article_exDel,
  article_exInfo
} from '@/api/project/article/project_article_article_ex'
export default {
  name: "a_articlea_article_ex_edit",
  components: {  },
  data() {
    return {
      curdAction: "",
      saveLoading: false,
      formData: {
        article_id: null, 
        platform: null, 
        view_control: null, 
        comment_control: null, 
        version_id: null, 
    },
      platformSelectData: [
        {value: 1,label: 'PC'},
        {value: 2,label: 'wap端'},
        {value: 3,label: 'APP'},
        {value: 4,label: '小程序'},
      ],
      view_controlSelectData: [
        {value: 1,label: '允许访客访问'},
        {value: 2,label: '登录后访问'},
      ],
      comment_controlSelectData: [
        {value: 1,label: '允许游客评论'},
        {value: 2,label: '登录后评论'},
        {value: 3,label: '不允许评论'},
      ],
    }
  },
  created(){
    var result = location.search.match(new RegExp("[\?\&]id=([^\&]+)", "i"));
    this.formData.article_id=result[1];
        this.onInfo(this.formData);
  },
  methods:{
    onSearch(){
      this.listQuery.page=1;
      this.getList();
    },
    onInfoPage(scope){
      var view = this.$store.state.permission.routesMap["/article/article_ex_info"];
      this.$router.push({ path: view.path, query: {m_id:view.code_aes,id:scope.row.article_id} })
    }, 
    reset(){
      this.formData = {
        article_id: null, 
        platform: null, 
        view_control: null, 
        comment_control: null, 
        version_id: null, 
      };
 
    },
    onSave(){
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.saveLoading = true;
          if (this.curdAction == "add") {
            article_exAdd(this.$qs.stringify(this.formData)).then(r => {
                this.formData.article_id=r.dataSingle;
                this.curdAction = 'update';
                this.saveLoading  = false;
                this.$notify({
                  title: "添加成功",
                  type: "success",
                  duration: 2000
                });
                var that = this;
                setTimeout(function(){ 
                  that.closeSelectedTag();
                },2000);
            }).catch(()=>{
              this.saveLoading=false;
            });
          } else if (this.curdAction == "update") {
            article_exUpdate(this.$qs.stringify(this.formData)).then(r => {
                this.saveLoading  = false;
                this.$notify({
                  title: "修改成功",
                  type: "success",
                  duration: 2000
                });
                var that = this;
                setTimeout(function(){ 
                  that.closeSelectedTag();
                },2000);
            }).catch(()=>{
              this.saveLoading=false;
            });
          }
        }
      });
    },
    onInfo(data){
        article_exInfo({id:data.article_id}).then(r=>{
          if(r.dataSingle==null){
            this.curdAction = "add";
            return false;
          }
            this.formData.article_id=r.dataSingle.article_id;
            this.formData.platform=r.dataSingle.platform;
            this.formData.view_control=r.dataSingle.view_control;
            this.formData.comment_control=r.dataSingle.comment_control;
            this.formData.version_id=r.dataSingle.version_id;
            this.curdAction = "update";
        });
    },
    closeSelectedTag() {
      var view = this.$store.state.permission.routesMap["/article/article_ex_edit"];
      this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
        if (this.isActive(view)) {
          this.toLastView(visitedViews, view)
        }
      })
    },
    toLastView(visitedViews, view) {
      const latestView = visitedViews.slice(-1)[0]
      if (latestView) {
        this.$router.push(latestView.fullPath)
      } else {
        if (view.name === 'Dashboard') {
          this.$router.replace({ path: '/redirect' + view.fullPath })
        } else {
          this.$router.push('/')
        }
      }
    },
    isActive(route) {
      return route.path === this.$route.path
    },
  }
}
</script>